package com.example.dao.impl;

import com.example.dao.CityAreaDao;
import com.example.entity.CityArea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class CityAreaDaoImpl implements CityAreaDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public List<CityArea> getAll() {
        String sql = "select id,name from cityarea";
        List<CityArea> cityAreas = this.jdbcTemplate.query(sql, new RowMapper<CityArea>() {
            @Override
            public CityArea mapRow(ResultSet rs, int rowNum) throws SQLException {
                return CityArea.builder()
                        .id(rs.getInt("id"))
                        .name(rs.getString("name"))
                        .build();
            }
        });
        return cityAreas;
    }
}
